﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SpeechLib;
using System.Windows.Forms;
using System.IO;
namespace graduation_project.Voice_Recognition
{
    class ReconginiteRecord
    {
        private static ReconginiteRecord reco = null;
        private const int grammarId = 6;
        private SpInProcRecoContext RecoContext = null;  //独占识别器
        private ISpeechRecoGrammar Grammar = null;
        private string RecordXml = "E://WorkSpace//c++//graduation project//graduation project//Xml//Dynamic.xml";                                
        private System.Windows.Forms.Control display;                 //输出文件
        private SpFileStream spInputFile;                            //输入文件
        /// <summary>
        /// 初始化独占语音识别并加载xml文件
        /// </summary>
        /// <param name="disResult"></param>
        /// <param name="spFileName"></param>
        public void RecordFile(Control disResult, String spFileName)
        {
            SpInprocRecognizer inprocRecognizer = new SpInprocRecognizer();                                                 //识别文件必须用独占识别器
            spInputFile = new SpFileStream();                                                                               //打开输入文件流
            SpeechStreamFileMode spFileMode = SpeechStreamFileMode.SSFMOpenForRead;                                         //输入文件打开方式
            display = disResult;
            spInputFile.Open(spFileName, spFileMode, true);
            inprocRecognizer.AudioInputStream = spInputFile;                                                                //识别器打开输入文件
            RecoContext = (SpInProcRecoContext)inprocRecognizer.CreateRecoContext();                                       //加载上下文语境
            Grammar = RecoContext.CreateGrammar(grammarId);
            Grammar.CmdLoadFromFile(RecordXml, SpeechLoadOption.SLODynamic);                                                  //读取语法文件
            RecoContext.Recognition += new _ISpeechRecoContextEvents_RecognitionEventHandler(inprocContextRecognition);    //识别成功事件处理
            RecoContext.State = SpeechRecoContextState.SRCS_Enabled;                                                       //打开识别上下文和语法
            Grammar.CmdSetRuleIdState(0, SpeechRuleState.SGDSActive);

            RecoContext.EndStream += new _ISpeechRecoContextEvents_EndStreamEventHandler(RecoContext_EndStream);           //读文件结束事件，做一些关闭工作
        }
        /// <summary>
        /// 输出识别内容
        /// </summary>
        /// <param name="iIndex"></param>
        /// <param name="obj"></param>
        /// <param name="type"></param>
        /// <param name="result"></param>
        private void inprocContextRecognition(int iIndex, object obj, SpeechLib.SpeechRecognitionType type, SpeechLib.ISpeechRecoResult result)
        {
            if ((System.Decimal)result.PhraseInfo.GrammarId == grammarId )                   //识别成功，并且为当前语境下语法
                display.Text += result.PhraseInfo.GetText(0, -1, true) + Environment.NewLine;   //输出识别结果
        }
        /// <summary>
        /// 关闭识别，识别结束处理
        /// </summary>
        /// <param name="StreamNumber"></param>
        /// <param name="StreamPosition"></param>
        /// <param name="StreamReleased"></param>
        private void RecoContext_EndStream(int StreamNumber, object StreamPosition, bool StreamReleased)
        {
            Grammar.CmdSetRuleIdState(0, SpeechRuleState.SGDSInactive);                                                  //关闭语法器，词法器
            RecoContext.State = SpeechRecoContextState.SRCS_Disabled;
            spInputFile.Close();                                                                                              //关闭输入文件
        }
        public static ReconginiteRecord instance()
        {
            if (reco == null)
                reco = new ReconginiteRecord();
            return reco;
        }
    }
}
